package com.yuncheng.spcyApi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.entity.SysUserGzjl;

import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.SysUserGzjlMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.service.ISysUserGzjlService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;

import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 系统-用户工作经历
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SysUserGzjlServiceImpl extends BaseServiceImpl<SysUserGzjlMapper, SysUserGzjl> implements ISysUserGzjlService {

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Override
    public IPage getPageList(Page<SysUserGzjl> page, SysUserGzjl sysUserGzjl, String year, boolean isSelectMe, OrderItemVo orderItemVo) {
        LambdaQueryWrapper<SysUserGzjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sysUserGzjl.getFuserid()), SysUserGzjl::getFuserid,sysUserGzjl.getFuserid());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFcsgz()),SysUserGzjl::getFcsgz,sysUserGzjl.getFcsgz());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFgzbm()),SysUserGzjl::getFgzbm,sysUserGzjl.getFgzbm());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFgzdw()),SysUserGzjl::getFgzdw,sysUserGzjl.getFgzdw());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFzc()),SysUserGzjl::getFzc,sysUserGzjl.getFzc());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFzw()),SysUserGzjl::getFzw,sysUserGzjl.getFzw());
        queryWrapper.like(StringUtils.isNotBlank(sysUserGzjl.getFusermc()),SysUserGzjl::getFusermc,sysUserGzjl.getFusermc());
        if (sysUserGzjl.getFgzkssj() != null){
            queryWrapper.ge(SysUserGzjl::getFgzkssj,sysUserGzjl.getFgzkssj());
        }
        if (sysUserGzjl.getFgzjssj() != null){
            queryWrapper.le(SysUserGzjl::getFgzjssj,sysUserGzjl.getFgzjssj());
        }
        if (isSelectMe){
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser != null){
                queryWrapper.eq(StringUtils.isNotBlank(currentUser.getId()), SysUserGzjl::getFuserid,currentUser.getId());
            }
        }
        if (StringUtils.isNotBlank(year)){
//            queryWrapper.apply("(to_char(fgzkssj,'yyyy') = {0} or to_char(fgzjssj,'yyyy') = {1})",year, year);
            queryWrapper.like(SysUserGzjl::getFgzkssj, year);
        }
        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByAsc(SysUserGzjl::getFgzkssj);
            queryWrapper.orderByDesc(SysUserGzjl::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<SysUserGzjl> pageList = page(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<SysUserGzjl> getList(SysUserGzjl sysUserGzjl, String year, boolean isSelectMe) {
        LambdaQueryWrapper<SysUserGzjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(sysUserGzjl.getFuserid()), SysUserGzjl::getFuserid,sysUserGzjl.getFuserid());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFcsgz()),SysUserGzjl::getFcsgz,sysUserGzjl.getFcsgz());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFgzbm()),SysUserGzjl::getFgzbm,sysUserGzjl.getFgzbm());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFgzdw()),SysUserGzjl::getFgzdw,sysUserGzjl.getFgzdw());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFzc()),SysUserGzjl::getFzc,sysUserGzjl.getFzc());
        queryWrapper.like(StringUtils.isNotEmpty(sysUserGzjl.getFzw()),SysUserGzjl::getFzw,sysUserGzjl.getFzw());
        queryWrapper.like(StringUtils.isNotBlank(sysUserGzjl.getFusermc()),SysUserGzjl::getFusermc,sysUserGzjl.getFusermc());
        if (sysUserGzjl.getFgzkssj() != null){
            queryWrapper.le(SysUserGzjl::getFgzkssj,sysUserGzjl.getFgzkssj());
        }
        if (sysUserGzjl.getFgzjssj() != null){
            queryWrapper.ge(SysUserGzjl::getFgzjssj,sysUserGzjl.getFgzjssj());
        }

        if (isSelectMe){
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            if (currentUser != null){
                queryWrapper.eq(StringUtils.isNotBlank(currentUser.getId()), SysUserGzjl::getFuserid,currentUser.getId());
            }
        }

        if (StringUtils.isNotBlank(year)){
//            queryWrapper.apply("(to_char(fgzkssj,'yyyy') = {0} or to_char(fgzjssj,'yyyy') = {1})",year,year);
            queryWrapper.like(SysUserGzjl::getFgzkssj, year);
        }

        queryWrapper.orderByAsc(SysUserGzjl::getFgzkssj);
        queryWrapper.orderByDesc(SysUserGzjl::getCreateTime);
        List<SysUserGzjl> pageList = list(queryWrapper);
        return pageList;
    }

    @Override
    public SysUserGzjl addGzjl(SysUserGzjl gzjl) {
        gzjl.setId(GetUuIdUtils.ReplaceUuId());
        gzjl.setCreateTime(new Date());
        save(gzjl);

        return gzjl;
    }

    @Override
    public void updateGzjl(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isEmpty(buffer.get("id"))){
            throw new BusinessException("参数为空！");
        }
        String id = buffer.get("id");
        SysUserGzjl oldDjjbxxUpdate = getById(id);
        if (oldDjjbxxUpdate == null){
            throw new BusinessException("没有相匹配数据！");
        }

        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(oldDjjbxxUpdate, buffer);
        updateById(oldDjjbxxUpdate);
    }
}
